#include "powermanagementpage.h"
#include "ui_powermanagementpage.h"
#include <QMessageBox>
#include <QLineEdit>
#include <QInputDialog>

PowerManagementPage::PowerManagementPage(QWidget *parent) :
    MyWidget(parent),
    ui(new Ui::PowerManagementPage)
{
    ui->setupUi(this);
    loadPowerData();  // 加载电量信息
}

PowerManagementPage::~PowerManagementPage()
{
    delete ui;
}

void PowerManagementPage::loadPowerData()
{
    // 从数据库获取电量信息数据
    QList<QVariantList> powerData = SqlHelper::getInstance().queryPowerUsage();

    // 清空表格内容并设置列数
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "宿舍ID" << "电量使用" << "更新时间");

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); // 设置最后一列拉伸

    // 填充表格数据
    for (const QVariantList &data : powerData) {
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);

        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(data[1].toString()));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(data[2].toDouble(), 'f', 2))); // 电量显示为浮动类型
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(data[3].toString()));
    }
}

void PowerManagementPage::on_pushButtonInsert_clicked()
{
    // 弹出插入电量信息的对话框
    bool ok;
    QString dormitoryId = QInputDialog::getText(this, "新增电量信息", "宿舍ID:", QLineEdit::Normal, "", &ok);
    if (!ok || dormitoryId.isEmpty()) return;

    double powerUsage = QInputDialog::getDouble(this, "新增电量信息", "电量使用:", 0, 0, 100000, 2, &ok);
    if (!ok) return;

    if (SqlHelper::getInstance().insertPowerUsage(dormitoryId.toInt(), powerUsage)) {
        QMessageBox::information(this, "Success", "电量信息新增成功！");
        loadPowerData();  // 重新加载数据
    } else {
        QMessageBox::critical(this, "Error", "电量信息新增失败！");
    }
}

void PowerManagementPage::on_pushButtonModify_clicked()
{
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow == -1) {
        QMessageBox::warning(this, "修改失败", "请选择一行数据进行修改");
        return;
    }

    QString dormitoryId = ui->tableWidget->item(currentRow, 0)->text();
    double currentPowerUsage = ui->tableWidget->item(currentRow, 1)->text().toDouble();

    bool ok;
    double newPowerUsage = QInputDialog::getDouble(this, "修改电量信息", "电量使用:", currentPowerUsage, 0, 100000, 2, &ok);
    if (!ok) return;

    if (SqlHelper::getInstance().updatePowerUsage(dormitoryId.toInt(), dormitoryId.toInt(), newPowerUsage)) {
        QMessageBox::information(this, "Success", "电量信息修改成功！");
        loadPowerData();  // 重新加载数据
    } else {
        QMessageBox::critical(this, "Error", "电量信息修改失败！");
    }
}

void PowerManagementPage::on_pushButtonDelete_clicked()
{
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow == -1) {
        QMessageBox::warning(this, "删除失败", "请选择一行数据进行删除");
        return;
    }

    QString dormitoryId = ui->tableWidget->item(currentRow, 0)->text();

    int ret = QMessageBox::warning(this, "确认删除", "确定要删除该条电量信息吗?", QMessageBox::Yes | QMessageBox::No);
    if (ret == QMessageBox::Yes) {
        if (SqlHelper::getInstance().deletePowerUsage(dormitoryId.toInt())) {
            QMessageBox::information(this, "Success", "电量信息删除成功！");
            loadPowerData();  // 重新加载数据
        } else {
            QMessageBox::critical(this, "Error", "电量信息删除失败！");
        }
    }
}
